﻿using Infrastructure.Web;
using MyData.Data.Repository;
using MyData.Dto;
using MyData.Models;
using MyDomain;
using MyRepository;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRepository
{
    public class DepartRepository : RepositoryBase<Depart>, IDepartRepository
    {
        public PageDto<DepartDto> GetDeparts(QueryListModel query, string name,string orderName)
        {
            using (var db = this.NewDB())
            {
                var deviceIP = ConfigurationManager.AppSettings["DeviceIp"].ToString();
                var departs = db.Query<Depart>().Where(c => c.Name.Contains(name)).ToList();
                if (orderName == "0")
                {
                    departs = departs.OrderByDescending(c => c.CreatedTime).ToList();
                }
                else if (orderName == "1")
                {
                    departs = departs.OrderByDescending(c => c.OrderNum).ToList();
                }
                else if (orderName == "2")
                {
                    departs = departs.OrderBy(c => c.IsShow).ThenByDescending(c=>c.CreatedTime).ToList();
                }
                var accessRecords = db.Query<AccessRecord>().OrderByDesc(c => c.CreatedTime).Where(c => c.CreatedTime >= DateTime.Now.Date);
                var device = db.Query<Mobile>().FirstOrDefault(c => c.Ip == deviceIP);
                if (device != null)
                {
                    accessRecords = accessRecords.Where(c => c.MobileId != device.Id);
                }

                var userIds = accessRecords.Where(c => c.UserType == 0).ToList().GroupBy(c => c.UserId).Select(c => c.FirstOrDefault()).ToList().Where(c => c.TurnOver == 0).Select(c => c.UserId).Distinct().ToList();
                var users = db.Query<UserInfo>().ToList();
                List<DepartDto> dtos = new List<DepartDto>();
                foreach (var depart in departs)
                {
                    if (depart.Id == Guid.Empty)
                    {
                        var visitorIds = accessRecords.Where(c => c.UserType == 1).ToList().GroupBy(c => c.UserId).Select(c => c.FirstOrDefault()).ToList().Where(c => c.TurnOver == 0).Select(c => c.UserId).Distinct().ToList();
                        var totalVis = db.Query<Visitor>().Where(c => c.EndDate >= DateTime.Now.Date).Count();
                        dtos.Add(new DepartDto() { Name = depart.Name, ArrivedNum = visitorIds.Count, TotalNum = totalVis, Id = Guid.Empty, IsShow = depart.IsShow, OrderNum = depart.OrderNum, CreatedTime = depart.CreatedTime });
                    }
                    else
                    {
                        var count = db.Query<UserInfo>().Where(c => userIds.Contains(c.Id) && c.DepartId == depart.Id).Count();
                        var total = db.Query<UserInfo>().Where(c => c.DepartId == depart.Id).Count();
                        dtos.Add(new DepartDto() { Name = depart.Name, ArrivedNum = count, TotalNum = total, Id = depart.Id, IsShow = depart.IsShow, OrderNum = depart.OrderNum, CreatedTime = depart.CreatedTime });
                    }
                }
                var result = dtos.AsQueryable().Paging(query.Limit, query.Offset);
                return new PageDto<DepartDto>()
                {
                    total = result.total,
                    rows = result.rows
                };
            }
        }

        public List<string> GetDepartIn(Guid Id)
        {
            using (var db = this.NewDB())
            {
                var accessRecords = db.Query<AccessRecord>().OrderByDesc(c => c.CreatedTime).Where(c => c.CreatedTime >= DateTime.Now.Date);
                var deviceIP = ConfigurationManager.AppSettings["DeviceIp"].ToString();
                var device = db.Query<Mobile>().FirstOrDefault(c => c.Ip == deviceIP);
                if (device != null)
                {
                    accessRecords = accessRecords.Where(c => c.MobileId != device.Id);
                }
                if (Id != Guid.Empty)
                {
                    var userIds = accessRecords.Where(c => c.UserType == 0).ToList().GroupBy(c => c.UserId).Select(c => c.FirstOrDefault()).ToList().Where(c => c.TurnOver == 0).Select(c => c.UserId).Distinct().ToList();
                    return db.Query<UserInfo>().Where(c => userIds.Contains(c.Id)&&c.DepartId==Id).Select(c => c.NickName).ToList();
                }
                else
                {
                    var userIds = accessRecords.Where(c => c.UserType == 1).ToList().GroupBy(c => c.UserId).Select(c => c.FirstOrDefault()).ToList().Where(c => c.TurnOver == 0).Select(c => c.UserId).Distinct().ToList();
                    return db.Query<Visitor>().Where(c => userIds.Contains(c.Id)).Select(c => c.Name).ToList();
                }
             

            }
        }


    }
}
